# -*- encoding=utf8 -*-
__author__ = "hyq"

from airtest.core.api import *
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
from airtest.core.android.android import *
import wx

open = {'w1':False,
        'w2':True,
        'w4':True,
        'w5':True,
        'w6':True,
        'w7':True,
        'w9':False,
        'w10':False,
        'r290':False}

def sendweixin(message_dict,debug=False):
    poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
    #dev = connect_device('Android:///192.168.8.102:7555')
    dev = connect_device('Android:///3HX5T16B29007993')
    poco = AndroidUiautomationPoco(dev)
    keyevent("HOME")
    start_app('com.tencent.mm')
    poco(text="微信").click()
    text_1 = []
    text_2 = []
    for line,text in message_dict.items():
        if line in ['w1','w2','w4','w5','w6'] and open[line]:
            text_2.append(text)
        elif line in ['w7','w9','w10','r290'] and open[line]:
            text_1.append(text)
    if debug is False:
        input_text('外二车间', text_2, poco)
        input_text('外一车间', text_1, poco)
    else:
        input_text('外二车间', text_2, poco, debug=True)
        input_text('外一车间', text_1, poco, debug=True)
    keyevent("HOME")


def input_text(workshop,texts, poco,debug=False):
    time.sleep(1)
    if debug is False:
        poco(text="{0}急停分析结果".format(workshop)).click()
    else:
        poco(text="{0}急停分析结果test".format(workshop)).click()
    poco("com.tencent.mm:id/g2p").click()
    for txt in texts:
        t = txt.split("\n")
        for m in t:
            text(m,enter=False)
            keyevent("ENTER")
        poco(text="发送").click()
    # keyevent("BACK")
    keyevent("BACK")
    keyevent("BACK")
    auto_setup(__file__)


def send(message_dict,debug=False):
    for line, text in message_dict.items():
        if line in ['w1','w2','w4','w5','w6'] and open[line]:
            wx.sendText('外二车间急停分析结果test',text)
        elif line in ['w7','w9','w10','r290'] and open[line]:
            wx.sendText('外一车间急停分析结果test', text)



if __name__ == '__main__':
    sendweixin('{0}{1}{2}短暂停通告：{3}在过去10分钟内急停最多，有{4}次，请线长到现场处理。'.format('W10','白班','3月8日','卤检急停','11'))